package com.gree.changying.activity.base;


import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.githang.statusbar.StatusBarCompat;
import com.gree.changying.R;


/**
 * Create by 游德禄 on 2019-9-3
 * 子页面基础页
 */
public abstract class BasePageActivity extends BaseActivity {

    /**
     * 主容器，左边按钮，右边按钮，消息盒子，菜单
     */
    private LinearLayout main,leftBtn,rightBtn,menu;
    /**
     * 左边按钮图标，右边按钮图标，信息图标
     */
    public ImageView leftImg,rightImg;
    /**
     * 顶部容器
     */
    private RelativeLayout header;
    /**
     * 标题，消息文本,左边按钮标题，右边按钮标题
     */
    private TextView title,leftText,rightText;
    /**
     * 内容区
     */
    private ViewFlipper v;


    private View.OnClickListener onClickListener;

    private int statusColor = 0 ;
    private boolean statusLight ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(setStatusBar()) {
            setStatusBar(R.color.themeColor, false);
        }
        super.setContentView(R.layout.activity_base_page);
        v = findViewById(R.id.layout_container);
        main= findViewById(R.id.main);
        leftBtn= findViewById(R.id.leftBtn);
        rightBtn= findViewById(R.id.rightBtn);
        leftImg= findViewById(R.id.leftImg);
        rightImg= findViewById(R.id.rightImg);
        header= findViewById(R.id.header);
        title= findViewById(R.id.title);
        leftText= findViewById(R.id.leftText);
        rightText= findViewById(R.id.rightText);
        onClickListener = view -> {
            switch (view.getId()){
                case R.id.leftBtn:
                    if(onFinish()){
                        finish();
                    }
                    break;
                case R.id.rightBtn:
                     onRightBtnClick(rightBtn);
                    break;
            }
        };

        leftBtn.setOnClickListener(onClickListener);
        rightBtn.setOnClickListener(onClickListener);
    }
    @Override
    public void setContentView(View view) {
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        v.addView(view, lp);
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    /**
     * 设置状态栏颜色
     * @param color
     * @param light
     */
    public void setStatusBar(int color,boolean light){
        statusColor = color ;
        statusLight = light;
        StatusBar(color,light);
    }

    /**
     *  设置状态栏透明
     */
    public void statusBarTranslucent(){
        StatusBarCompat.setTranslucent(getWindow(),true);
    }
    protected void StatusBar(int color,boolean light){
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(color), light);
    }
    public abstract boolean setStatusBar();
    //全屏
    public boolean FullScreen(boolean isLight){
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        setStatusBarLight(isLight);
        return false;
    }
    protected void setStatusBarLight(boolean isLight){
        Window window = getWindow();
        if(isLight) {
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
        }else{
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
        }

    }


    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title){
        this.title.setText(title);
    }

    /**
     * 设置左边按钮文字
     * @param text
     */
    public void setLeftText(String text){
        this.leftText.setVisibility(View.VISIBLE);
        this.leftText.setText(text);
    }
    /**
     * 设置右边按钮文字
     * @param text
     */
    public void setRightText(String text){
        this.rightText.setVisibility(View.VISIBLE);
        this.rightText.setText(text);
    }

    /**
     * 设置左边按钮图标
     * @param r
     */
    public void setLeftImg(int r){
        this.leftImg.setImageResource(r);
    }

    /**
     * 设置右边按钮图标
     * @param r
     */
    public void setRightImg(int r){
        this.rightImg.setImageResource(r);
    }

    /**
     * 隐藏左边按钮
     */
    public void hideLeftBtn(){
        this.leftBtn.setVisibility(View.GONE);
    }

    /**
     * 隐藏右边按钮
     */
    public void hideRightBtn(){
        this.rightBtn.setVisibility(View.GONE);
    }

    /**
     * 隐藏标题栏
     */
    public void hideHeader(){
        this.header.setVisibility(View.GONE);
    }
    /**
     * 设置标题栏背景
     * @param r
     */
    public void setHeaderBg(int r){
        this.header.setBackgroundResource(r);
    }

    /**
     * 由子类决定是否关闭页面
     * @return
     */
    public abstract boolean onFinish();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(onFinish()){
                finish();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 右边按钮点击
     * @return
     */
    public abstract void onRightBtnClick(View view);



    //判断是否是全屏
    private boolean isTransparentBar(){
        return (getWindow().getStatusBarColor()== Color.TRANSPARENT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
